package com.example.localexceptionhandler.handler;

import com.example.localexceptionhandler.enums.CommonExceptionEnum;
import com.example.localexceptionhandler.exception.XJRuntimeException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @version 1.0
 * @program: localexceptionhandler
 * @packageName: com.example.localexceptionhandler.handler
 * @className XJExceptionHandler
 * @description: TODO
 * @author: xj
 * @create: 2021-01-18 11:51:52
 **/
@ControllerAdvice
public class XJExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public String handlerException(HttpServletRequest request, Exception ex){
        if(ex instanceof XJRuntimeException){
            XJRuntimeException xjRuntimeException= (XJRuntimeException) ex;
            return xjRuntimeException.getDisplayName();
        }else{
            return CommonExceptionEnum.COMMON_EXCPETION.getDisplayName();
        }
    }
}
